package handlers;

import java.net.Socket;
import java.util.ArrayList;

import main_classes.Server;

import common.classes.FileNameWorker;
import common.classes.HeaderWorker;
import common.classes.RedirectResponseThread;

public class RedirectHandler extends Handler {
	String path;
	ArrayList<String> subList;

	public RedirectHandler() {
		super();
		this.path = "datas/";
		this.subList = Server.getSettings().getSubsidiariesServers();
	}

	public RedirectHandler(Handler next, String path, ArrayList<String> subList) {
		super(next);
		this.path = path;
		this.subList = subList;
	}
	
	@Override
	public void parseRequest(Socket socket, HeaderWorker header, String error) {
		if (error != null) {
			super.parseRequest(socket, header, error);
			return;
		}
		String filePath = FileNameWorker.createFullFilePath(this.path, header.getFilePath());
		if (filePath.equals("")) {
			Thread th = new Thread(new RedirectResponseThread(socket, this.subList, header.getFilePath()));
			th.start();
			return;
		}
		super.parseRequest(socket, header, error);
	}

}
